package com.bluefox.config;

import com.bluefox.utils.SecurityUtils;
import com.bluefox.modules.system.domain.SysDept;
import com.bluefox.modules.system.service.SysDeptService;
import com.bluefox.modules.system.service.SysRoleService;
import com.bluefox.modules.system.service.SysUserService;
import com.bluefox.modules.system.service.dto.RoleSmallDto;
import com.bluefox.modules.system.service.dto.SysDeptDto;
import com.bluefox.modules.system.service.dto.SysUserDto;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName com.bluefox.config
 * @Description com.bluefox.config
 * @Author 孙峰
 * @Date 2020/4/17 13:32
 * @Version 1.0
 */
@Component
public class DataScope {
    /**
     * 全部数据权限 1
     * 本级数据权限 2
     * 自定数据权限 3
     */
    private final String[] scopeType = {"1","2","3"};

    private final SysUserService userService;

    private final SysRoleService roleService;

    private final SysDeptService deptService;

    public DataScope(SysUserService userService, SysRoleService roleService, SysDeptService deptService) {
        this.userService = userService;
        this.roleService = roleService;
        this.deptService = deptService;
    }
    public Set<Long> getDeptIds() {
            //根据登录的用户名 获取登录用户
        SysUserDto user = userService.findByName(SecurityUtils.getUsername());

        // 用于存储部门id
        Set<Long> deptIds = new HashSet<>();

        // 查询用户角色
        List<RoleSmallDto> roleSet = roleService.findByUserId(user.getId());

        for (RoleSmallDto role : roleSet) {

            if (scopeType[0].equals(role.getDataScope())) {
                return new HashSet<>() ;
            }

            // 存储本级的数据权限
            if (scopeType[1].equals(role.getDataScope())) {
                deptIds.add(user.getDept().getId());
            }

            // 存储自定义的数据权限
            if (scopeType[2].equals(role.getDataScope())) {
                Set<SysDept> depts = deptService.findByRoleId(role.getId());
                for (SysDept dept : depts) {
                    deptIds.add(dept.getId());
                    List<SysDeptDto> deptChildren = deptService.findByPid(dept.getId());
                    if (deptChildren != null && deptChildren.size() != 0) {
                        deptIds.addAll(getDeptChildren(deptChildren));
                    }
                }
            }
        }
        return deptIds;
    }


    public List<Long> getDeptChildren(List<SysDeptDto> deptList) {
        List<Long> list = new ArrayList<>();
        deptList.forEach(dept -> {
                    if (dept!=null && dept.getStatus().equals("0")){
                        List<SysDeptDto> depts = deptService.findByPid(dept.getId());
                        if(deptList.size() != 0){
                            list.addAll(getDeptChildren(depts));
                        }
                        list.add(dept.getId());
                    }
                }
        );
        return list;
    }
}
